/*
 *所有关于pprecords_main类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*pprecords_mainService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VolPro.Core.BaseProvider;
using VolPro.Core.Extensions.AutofacManager;
using VolPro.Entity.DomainModels;
using System.Linq;
using VolPro.Core.Utilities;
using System.Linq.Expressions;
using VolPro.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using SMes.DbMethod.IRepositories;
using System.Numerics;
using System;

namespace SMes.DbMethod.Services
{
    public partial class pprecords_mainService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Ipprecords_mainRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public pprecords_mainService(
            Ipprecords_mainRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }
        /// <summary>
        /// 批次新建-开工作业
        /// </summary>
        /// <param name="LotNo">批次号</param>
        public async Task<WebResponseContent> LotStartWorking(string LotNo)
        {
            var item= await _repository.FindAsyncFirst(m => m.LotNo == LotNo);
            if (item == null)
                return new WebResponseContent().Error($"LotNo:{LotNo} No exit,Please update after adding batch data...");
            if(item.Status == 1)
                return new WebResponseContent().Error($"LotNo:{LotNo} The batch has been started, please do not repeat the operation...");
            item.Status = 1;     
            item.CreateTime = DateTime.Now;
            repository.Update(item, x => new { x.Status,x.CreateTime });
            repository.SaveChanges();
            return new WebResponseContent().OK($"update  {LotNo} status success... ");
        }
        /// <summary>
        /// 创建的批次删除-未开工作业
        /// </summary>
        /// <param name="planNo">计划号</param>
        /// <returns></returns>
        public async Task<WebResponseContent> LotDeletNoWorking(string planNo)
        {
            var item = await _repository.FindAsIQueryable(m => m.PlanNo == planNo && m.Status<1).ToListAsync(); ;
            if (item == null)
                return new WebResponseContent().Error($"planNo:{planNo} No exit,Please update after adding batch data...");
            object[] keys = [];
            foreach (var it in item)
            {
                keys.Append(it.LotID);
            }
            var delNum=repository.DeleteWithKeys(keys);
            return new WebResponseContent().OK($"delete {planNo} date amount {delNum}... ");
        }
    }
}
